import enquireJs from 'enquire.js'
import error from "@/pages/result/Error";

export function isDef (v){
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove (arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

/**
 * 获取字符串长度，英文字符 长度1，中文字符长度2
 * @param {*} str
 */
export function getStrFullLength (str = '') {
  str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128) {
      return pre + 1
    }
    return pre + 2
  }, 0)
}


export function isRegExp (v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function () {
      call && call(true)
    },
    unmatch: function () {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

export function filterArray(e) {
  return e.filter(item => item.Value != '')
}

export function isLabel(e) {
  let flag = true
  try {
    e.forEach(item => {
      if(item.DisplayType != 'label'){
        throw new Error("抛出异常跳出")
      }
    });
  } catch(e) {
    flag = false
  }
  return flag
}

export function numToTime(e) {
  if(e.indexOf(':') != -1) {
    return e
  }
  while (e.length < 4) {
    e = '0' + e
  }

  return e.slice(0, 2) + ':' + e.slice(2, 4)
}


export function trimAll(ele){
  if(typeof ele === 'string'){
    return ele.split(' ').join('');

  }else{
    console.error(`${typeof ele} is not the expected type, but the string type is expected`)
  }

}

//首字母大写转小写
export function convertJsonKey(jsonObj){
  var result = {};
  for(let key in jsonObj){
    var keyval = jsonObj[key];
    key = key.replace(key[0],key[0].toLowerCase());
    result[key] = keyval;
  }
  return result;
}


const _toString = Object.prototype.toString
